<?php

/**
 * @author      Nguyen Duc Thuan <me@ndthuan.com>
 * @copyright   Copyright (c) 2009 Nguyen Duc Thuan <me@ndthuan.com>
 * @version     $Id: DataSet.php 12 2009-11-03 03:17:46Z thuan.nguyen $
 */

class Fete_Model_DataSet
{
    protected $_currentIndex    = -1;
    protected $_maxIndex        = -1;
    protected $_rows            = array();
    protected $_numOfPages      = 1;
    protected $_numOfRows       = 0;
    protected $_pageNum         = 1;
    protected $_dataRowClassName = 'Fete_Model_DataRow';
    /**
     *
     * @var Fete_Model_DataRow
     */
    protected $_emptyDataRow    = null;

    public function __construct($rawRows = array())
    {
        $dataRowClassName = $this->_dataRowClassName;
        foreach ($rawRows as $row)
        {
            $dataRow = new $dataRowClassName($row);
            $this->_rows[] = $dataRow;
            ++$this->_maxIndex;
        }
        $this->_emptyDataRow = new $dataRowClassName();
        $this->init();
    }

    /**
     *
     * @param array $rows
     * @return Fete_Model_DataSet
     */
    static public function createInstance($rawRows = array())
    {
        return new self($rawRows);
    }

    /**
     *
     * @param array $row
     * @return Fete_Model_DataSet
     */
    public function &addRawRow($row)
    {
        $dataRow = new $this->_dataRowClassName($row);
        $this->_rows[] = $dataRow;
        ++$this->_maxIndex;

        return $this;
    }

    /**
     *
     * @param Fete_Model_DataRow $row
     * @return Fete_Model_DataSet
     */
    public function &addRow(Fete_Model_DataRow $row)
    {
        $this->_rows[] = $row;
        ++$this->_maxIndex;
        return $this;
    }

    /**
     *
     * @param array $rows
     * @return Fete_Model_DataSet
     */
    public function &setRawRows($rows)
    {
        $dataRows = array();
        foreach ($rows as $offset => $row)
        {
            $dataRow = new $this->_dataRowClassName($row);
            $dataRows[] = $dataRow;
        }
        $this->_rows = $dataRows;
        $this->_maxIndex = $offset;
        return $this;
    }

    /**
     *
     * @param array $rows
     * @return Fete_Model_DataSet
     */
    public function &setRows($rows)
    {
        $this->_rows = $rows;
        $this->_maxIndex = count($this->_rows) - 1;
        return $this;
    }

    /**
     *
     * @param integer $index
     * @return Fete_Model_DataSet
     */
    public function &setCurrentIndex($index)
    {
        $this->_currentIndex = $index;
        return $this;
    }

    /**
     *
     * @param integer $nop
     * @return Fete_Model_DataSet
     */
    public function &setNumOfPages($nop)
    {
        $this->_numOfPages = $nop;
        return $this;
    }

    /**
     *
     * @param integer $nor
     * @return Fete_Model_DataSet
     */
    public function &setNumOfRows($nor)
    {
        $this->_numOfRows = $nor;
        return $this;
    }

    /**
     *
     * @param integer $pn
     * @return Fete_Model_DataSet
     */
    public function &setPageNum($pn)
    {
        $this->_pageNum = $pn;
        return $this;
    }

    /**
     *
     * @return integer
     */
    public function getNumOfPages()
    {
        return $this->_numOfPages;
    }

    /**
     *
     * @return integer
     */
    public function getNumOfRows()
    {
        return $this->_numOfRows;
    }

    /**
     *
     * @return integer
     */
    public function getPageNum()
    {
        return $this->_pageNum;
    }

    /**
     * Moves the pointer forward
     * @return bool
     */
    public function read()
    {
        ++$this->_currentIndex;
        return $this->_currentIndex >= 0
        && $this->_currentIndex <= $this->_maxIndex
        && isset($this->_rows[$this->_currentIndex]);
    }

    /**
     *
     * @return Fete_Model_DataRow
     */
    public function &current()
    {
        if (isset($this->_rows[$this->_currentIndex])) {
            return $this->_rows[$this->_currentIndex];
        }
        return $this->_emptyDataRow;
    }

    /**
     *
     * @return Fete_Model_DataRow
     */
    public function &next()
    {
        $index = $this->_currentIndex + 1;
        if (isset($this->_rows[$index])) {
            $this->_currentIndex = $index;
            return $this->_rows[$index];
        }
        return $this->_emptyDataRow;
    }

    /**
     *
     * @return Fete_Model_DataRow
     */
    public function &prev()
    {
        $index = $this->_currentIndex - 1;
        if (isset($this->_rows[$index])) {
            $this->_currentIndex = $index;
            return $this->_rows[$index];
        }
        return $this->_emptyDataRow;
    }

    /**
     *
     * @return Fete_Model_DataSet
     */
    public function &reset()
    {
        $this->_currentIndex = -1;
        return $this;
    }

    /**
     *
     * @return boolean
     */
    public function isLast()
    {
        return $this->_currentIndex === $this->_maxIndex
        && isset($this->_rows[$this->_currentIndex]);
    }

    /**
     *
     * @return boolean
     */
    public function isFirst()
    {
        return 0 === $this->_currentIndex && isset($this->_rows[0]);
    }

    public function init() {}
}